<template>
    <div class="px-[5px] pt-[10px] text-right" v-if="props.showCheckText && procInsId != null">
        <el-link type="primary" @click="checkHistoryModalVisible = true">审核记录</el-link>
    </div>
    <div class="bg-white my-[10px]">
        <BaseHead>来款信息</BaseHead>
        <BaseTable>
            <tr>
                <td class="td-name">摘要</td>
                <td colspan="3">{{cwIncomeShow.note}}</td>
            </tr>
            <tr>
                <td class="td-name">来款金额</td>
                <td>{{FloatDiv(cwIncomeShow.feeValue,10000)}} <span>万元</span></td>
                <td class="td-name">来款类型</td>
                <td>
                    <CommonLabel :dataList="incomeTypeList" valueKey="code" :value="cwIncomeShow.incomeType" />
                </td>
            </tr>
            <tr>
                <td class="td-name">可认金额</td>
                <td>{{FloatDiv(cwIncomeShow.notClaimfee,10000) }} <span>万元</span></td>
                <td class="td-name">来款时间</td>
                <td>{{cwIncomeShow.incomeDate}}</td>
            </tr>
        </BaseTable>
    </div>
    <div class="bg-white my-[10px]">
        <BaseHead>项目信息</BaseHead>
        <BaseTable>
            <tr>
                <td class="td-name">项目名称</td>
                <td>{{claimProjectShow.projectName}}</td>
                <td class="td-name">负责人</td>
                <td>{{claimProjectShow.chargerName}} </td>
            </tr>
            <tr>
                <td class="td-name">项目未到账金额</td>
                <td colspan="3">{{ FloatDiv(claimProjectShow.notIncomeFee,10000) }} <span>万元</span></td>
            </tr>
            <tr>
                <td class="td-name">项目合同金额
                </td>
                <td colspan="3">{{claimProjectShow.projectFeeInfo }}</td>
            </tr>
        </BaseTable>
    </div>
    <div class="bg-white my-[10px]">
        <BaseHead style="border-bottom: 0">认领经费</BaseHead>
        <BaseTable>
            <tr>
                <td class="td-name">认领金额(万元)</td>
                <td>{{ FloatDiv(claimShow.claimFee,10000) }}</td>
                <td class="td-name">来款类型</td>
                <td>
                    <CommonLabel :dataList="incomeTypeList" valueKey="code" :value="claimShow.incomeType" />
                </td>
            </tr>
            <tr>
                <td class="td-name">外拨金额(万元)</td>
                <td colspan="3">{{ FloatDiv(claimShow.outboundFee,10000) }}</td>
            </tr>
            <tr>
                <td class="td-name">留校经费(万元)
                </td>
                <td colspan="3">{{FloatDiv(claimShow.schoolFee,10000) }}</td>
            </tr>
        </BaseTable>
    </div>
    <div class="bg-white my-[10px]">
        <BaseHead style="border-bottom: 0">建卡及额度</BaseHead>
        <BaseTable>
            <tr>
                <td class="td-name">经费卡负责人</td>
                <td colspan="3" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow" :key="index">
                    {{items.feeChargerName+"("+items.feeChargerCode+")"}}</td>
            </tr>
            <tr>
                <td class="td-name">所属单位</td>
                <td colspan="3" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow" :key="index">
                    <CommonTreeLabel :dataList="unitIdList" :value="items.unitId" valueKey="codeColumn"
                        labelKey="valueColumn" />
                </td>
            </tr>
            <tr>
                <td class="td-name">经费卡号</td>
                <td colspan="3" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow" :key="index">
                    {{items.feeCardNum?items.feeCardNum:"审核通过后自动生成"}}
                </td>
            </tr>
            <tr>
                <td class="td-name">留校经费(万元)</td>
                <td colspan="3" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow" :key="index">
                    {{ FloatDiv(items.schoolFee,10000)}}</td>
            </tr>
            <tr>
                <td class="td-name">外拨经费(万元)</td>
                <td colspan="3" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow" :key="index">
                    {{FloatDiv(items.outboundFee,10000)}}</td>
            </tr>
        </BaseTable>
    </div>
    <div class="bg-white my-[10px]">
        <BaseTable v-if="claimShow.budgetSplitModde =='liuxiaoBudgetSplit' && budgetshow">
            <tr>
                <td class="td-subname"></td>
                <td class="td-subname"></td>
                <td class="td-subname" colspan="6" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow"
                    :key="index">
                    {{items.feeChargerName+"("+items.feeChargerCode+")"}}</td>
            </tr>
            <tr>
                <td class="td-subname">额度名称</td>
                <td class="td-subname">留校总额度(万元)</td>
                <td class="td-subname" colspan="6">额度拆分详情(万元)</td>
            </tr>
            <tr v-for="(items,index) in claimFeeShow.claimBudgetFeeShow" :key="index">
                <td class="td-subname">{{items.subjectName}}</td>
                <td class="td-subname">{{FloatDiv(items.subjectFee,10000)}}</td>
                <td colspan="3" v-for="(item,index) in items.claimSubBudgetFeeShow" :key="index">
                    {{FloatDiv(item.budgetFee,10000)}}
                </td>
            </tr>
        </BaseTable>
        <BaseTable v-if="claimShow.budgetSplitModde =='incomeBudgetSplit' && budgetshow">
            <tr>
                <td class="td-subname"></td>
                <td class="td-subname"></td>
                <td class="td-subname" colspan="6" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow"
                    :key="index">
                    {{items.feeChargerName+"("+items.feeChargerCode+")"}}</td>
            </tr>
            <tr>
                <td class="td-subname">额度名称</td>
                <td class="td-subname">留校总额度(万元)</td>
                <td class="td-subname" colspan="3">累计入账额度(万元)</td>
                <td class="td-subname" colspan="3">本次额度拆分(万元)</td>
            </tr>
            <tr v-for="(items,index) in claimFeeShow.claimBudgetFeeShow" :key="index">
                <td class="td-subname">{{items.subjectName}}</td>
                <td class="td-subname">{{FloatDiv(items.subjectFee,10000)}}</td>
                <td colspan="3" v-for="(item,index) in items.claimSubBudgetFeeShow" :key="index">
                    {{FloatDiv(item.splitBudgetFee,10000)}}
                </td>
                <td colspan="3" v-for="(item,index) in items.claimSubBudgetFeeShow" :key="index">
                    {{ FloatDiv(item.budgetFee,10000)}}
                </td>
            </tr>
        </BaseTable>
    </div>
    <div class="bg-white my-[10px]">
        <BaseTable v-show="manageShow">
            <tr>
                <td class="td-subname"></td>
                <td class="td-subname"></td>
                <td class="td-subname" colspan="6" v-for="(items,index) in claimFeeShow.claimIncomeFeeShow"
                    :key="index">
                    {{items.feeChargerName+"("+items.feeChargerCode+")"}}</td>
            </tr>
            <tr>
                <td class="td-subname">管理费信息</td>
                <td class="td-subname">-</td>
                <td class="td-subname" colspan="3">累计扣除管理费(万元)</td>
                <td class="td-subname" colspan="3">本次扣除管理费(万元)</td>
            </tr>
            <tr v-for="(items,index) in claimFeeShow.claimManageFeeShow" :key="index">
                <td class="td-subname">{{items.subjectName}}</td>
                <td class="td-subname">-</td>
                <td colspan="3" v-for="(item,index) in items.claimSubManageFee" :key="index">
                    {{FloatDiv(item.splitManageFee,10000)}}
                </td>
                <td colspan="3" v-for="(item,index) in items.claimSubManageFee" :key="index">
                    {{FloatDiv(item.subjectFee,10000)}}
                </td>
            </tr>
        </BaseTable>
    </div>
    <CheckHistoryModal  v-model="checkHistoryModalVisible" :procInsId="procInsId" :checker="checker"
        :checkDate="checkDate" />
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import BaseTable from "@/views/fee/components/BaseTable.vue";
import CommonLabel from "@/views/project/components/CommonLabel.vue";
import CommonTreeLabel from "@/views/project/components/CommonTreeLabel.vue";
import CheckHistoryModal from "@/views/project/components/CheckHistoryModal.vue";
import { FloatDiv } from "@/utils/pub";
import { getClaimFeeInfo,getManage,getBudegt,getInfo } from "@/api/fee/rz/yhdk/index.js";
import useCommonFetch from "@/hooks/useCommonFetch";
const checkHistoryModalVisible = ref(false);
const cwIncomeShow = ref({}); //来款信息
const claimProjectShow = ref({}); //项目信息
const claimShow = ref({}); //认领经费
const claimFeeShow = ref({claimIncomeFeeShow:[]});//建卡费模块
const unitIdList = useCommonFetch("所有单位"); //所有单位; 
const incomeTypeList = useCommonFetch("来款类型");//来款类型
const showclaimFee = ref(false); //可认领金额展示
const showIncomeType = ref(true); //经费类型
const budgetshow = ref(false); //预算拆分科目展示
const manageShow = ref(false); //管理费展示
const procInsId = ref(null); //流程实例id
const checker = ref("");
const checkDate = ref("");
const props = defineProps({
  row: Object,
  showCheckText: {
    type: Boolean,
    default: false, //默认不显示审核记录文字
  },
});

const init = async () => {
    claimProjectShow.value.projectName = props.row.projectName;
    claimProjectShow.value.projectId = props.row.projectId;
    claimShow.value.id = props.row.operateId;
    claimShow.value = props.row;
    initCwIncomeInfo(props.row.cwIncomeId);
    initClaimFeeInfo(props.row.projectId,props.row.cwIncomeId,props.row.id);
    procInsId.value = props.row.processInstanceId;
    checker.value = props.row.checker;
    checkDate.value = props.row.checkdate;
};

// 来款信息
const initCwIncomeInfo = async (cwIncomeId)=>{
    let params = {Id:cwIncomeId};
    try {
        const res = await getInfo(params);
        if (res.code == 200) {
            cwIncomeShow.value = res.data;  
        }
    }catch (error) {
        console.log(error);
    }
}

//初始化认领和项目相关的数据
const initClaimFeeInfo = async (projectId,cwIncomeId,claimId)=>{
    let params = {projectId:projectId,cwIncomeId:cwIncomeId,claimId:claimId};
    try {
        const res = await getClaimFeeInfo(params);
        if (res.code == 200) {
            claimProjectShow.value.notIncomeFee = res.data.notIncomeFee; //项目未到账金额
            claimProjectShow.value.projectFeeInfo = res.data.projectFeeInfo;//项目合同金额展示信息
            claimProjectShow.value.chargerName = res.data.chargerName; //负责人 
            claimFeeShow.value.projectClass = res.data.projectClass; //项目类型
            claimFeeShow.value.planOutboundFee =res.data.planOutboundFee;//计划外拨
            claimFeeShow.value.outboundFee = res.data.outboundFee; //已外拨
            showclaimFee.value = true;//展示可认领多少金额提示
            claimFeeShow.value.feeSchemeId = res.data.feeSchemeId;//管理费提取方案id
            claimFeeShow.value.claimIncomeFeeShow = res.data.claimIncomeFeeShow; //经费卡号
            //留校预算是否已拆分标识,如果已拆分，则不在拆分 即只拆分一次
            //如果是拆分本次到账预算，则该值默认为false
            //该值等于true时，提交表单时无需填写预算，后台也无需接收预算，否则要校验提交表单时必须填写预算信息
            claimFeeShow.value.hasCardLiuxiaoBudget = res.data.hasCardLiuxiaoBudget;
            claimShow.value.budgetSplitModde = res.data.budgetSplitModde;//分割方式  
            handleContract(res.data.projectClass);
            showSplitIncomeFeeView(projectId);
            if(res.data.projectClass == "zXProject"){
                manageShow.value = true;
                getCliamManageFee(projectId);
            }
               
        }
    }catch (error) {
        console.log(error);
    }
}

//显示经费卡拆分查看页面
const showSplitIncomeFeeView = (projectId)=>{
    if (claimShow.value.budgetSplitModde == "incomeBudgetSplit" || claimFeeShow.value.hasCardLiuxiaoBudget) {
        budgetshow.value = true;
        getCliamIncomeBudget(projectId);  
    }
}

//初始化预算科目
const getCliamIncomeBudget = async (projectId)=>{
    var personIds = claimFeeShow.value.claimIncomeFeeShow.map(item => item.feeChargerId).join(",");
    let params={projectId:projectId,projectClass:claimFeeShow.value.projectClass,
        budgetSplitModde:claimShow.value.budgetSplitModde,personIds:personIds,claimId:claimShow.value.id};
    try {
        const res = await getBudegt(params);
        if (res.code == 200) {
            claimFeeShow.value.claimBudgetFeeShow = res.data;
        }
    }catch (error) {
        console.log(error);
    }
}

//初始化管理费
const getCliamManageFee = async (projectId)=>{
    let params = {projectId:projectId,claimId:claimShow.value.id};
    try {
        const res = await getManage(params);
        if (res.code == 200) {
            claimFeeShow.value.claimManageFeeShow = res.data;
        }
    }catch (error) {
        console.log(error);
    }
}

//横向项目处理:不体现来款类型、是否创建新卡；默认为混合经费
const handleContract = async (projectClass)=>{
    if (projectClass == 'yfContract') { // 横向项目
        showIncomeType.value = false;
		claimShow.value.incomeType= "mixed";
	}
}
onMounted(async () => {
  await init();
});
</script>

<style scoped lang="scss"></style>